// pages/balance/balance.js
const memberService = require('../../../../api/memberService.js')
const auth = require('../../../../utils/auth.js')

//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    balanceList: [],
    page: 1,
    mobile: '',
    pageSize: 10,
    pageCount: 1,
    noMore: false,
    loading: false
  },
  // 检查当前用户是否登录授权
  checkAuth: function () {
    try {
      if (!app.globalData.userInfo) {
        return false;
      } else {
        return true;
      }
    } catch (e) {
      // Do something when catch error
    }
  },

  // 操作记录
  handleAction: function (value) {
    if (value === 'recharge') {
      return '充值'
    } else if (value === 'consume') {
      return '消费'
    } else if (value === 'adjust') {
      return '调整'
    } else if (value === 'checkIn') {
      return '签到'
    } else if (value === 'refund') {
      return '退款'
    } else if (value === '充值') {
      return '充值'
    } else if (value === '消费') {
      return '消费'
    } else if (value === '调整') {
      return '调整'
    } else if (value === '签到') {
      return '签到'
    } else if (value === '退款') {
      return '退款'
    } else {
      return value
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu()
    const that = this
    if (app.globalData.userInfo) {
      if (app.globalData.userInfo.member) {
        that.setData({
          member: app.globalData.userInfo.member,
          mobile: app.globalData.userInfo.member.mobile
        })
        that.handelQueryList(this.data.page);
      } else {
        wx.showToast({
          title: '您还不是会员，请先绑定手机号码成为会员',
          icon: 'none',
          duration: 2000
        })
      }
    } else {
      try {
        wx.showModal({
          title: '提示',
          content: '请登录后查看',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.navigateTo({
                url: '../my/my'
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } catch (error) { }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      balanceList: [],
      mobile: '',
      page: 1,
      pageCount: 1,
      noMore: false
    })
    this.onLoad();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.page < this.data.pageCount) {
      // 如果查询结果数量不为0，说明还有下一页，执行查询操作
      this.setData({
        loading: true
      })
      let page = this.data.page;
      ++page;
      this.handelQueryList(page);
    } else {
      // 没有下一页，提示已经查询完毕
      if (this.data.balanceList.length > 0) {
        this.setData({
          noMore: true
        })
      } else {
        wx.showToast({
          title: '没有更多数据~',
          icon: 'none',
          duration: 2000
        })
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 调用接口获取储值明细
   */
  handelQueryList: function (page) {
    let postData = {
      memberMobileEquals: this.data.mobile,
      page: page,
      pageSize: this.data.pageSize,
      orderDirection: 'desc',
      orderField: "tranTime",
    }
    const that = this
    memberService.query(postData).then(res => {
      // console.log(res)
      if (res && res.records && res.records.length >= 0) {
        let tempBalanceList = this.data.balanceList;
        res.records.forEach(item => {
          let tempState = true; // true 表示正数，false表示负数
          let occurTotal = '+￥0.00'
          if (item.occurTotal < 0 || item.action === '消费' || item.action === 'consume') {
            tempState = false
            let tempTotal = 0 - item.occurTotal
            occurTotal = '-￥' + tempTotal
          } else {
            tempState = true
            occurTotal = '+￥' + item.occurTotal
          }

          tempBalanceList.push({
            ...item,
            action: this.handleAction(item.action),
            state: tempState,
            occurTotal: occurTotal
          })
        })
        let pageCount = parseInt(res.recordCount / res.pageSize) + 1;
        this.setData({
          page: res.page,
          pageCount: pageCount,
          balanceList: tempBalanceList
        })
      }
      if (this.data.loading) {
        that.setData({
          loading: false
        })
      }
    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none',
        duration: 2000
      })
      if (this.data.loading) {
        that.setData({
          loading: false
        })
      }
    })
  }
})